package com.honglv.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.honglv.constant.LikeContext;
import com.honglv.context.BaseContext;
import com.honglv.mapper.StrategyMapper;
import com.honglv.pojo.dto.PageByCityDTO;
import com.honglv.pojo.dto.PageDTO;
import com.honglv.pojo.dto.UploadStrategyDTO;
import com.honglv.pojo.entity.Strategy;

import com.honglv.pojo.vo.GetStrategiesByCityVO;
import com.honglv.pojo.dto.UpdateStrategyDTO;
import com.honglv.pojo.result.Result;
import com.honglv.pojo.vo.GetAllStrategiesVO;
import com.honglv.pojo.vo.GetStrategyVO;
import com.honglv.pojo.vo.PageVO;
import com.honglv.service.StrategyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class StrategyServiceImpl implements StrategyService {
    @Autowired
    private StrategyMapper strategyMapper;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 用户上传攻略
     * @param uploadStrategyDTO
     * @return
     */
    @Override
    public void upload(UploadStrategyDTO uploadStrategyDTO) {
        Long authorId = BaseContext.getCurrentId();
        Strategy strategy=new Strategy();
        BeanUtils.copyProperties(uploadStrategyDTO,strategy);
        strategy.setAuthorId(authorId);
        strategy.setLike(0L);
        strategy.setViews(0L);
        strategyMapper.insert(strategy);
    }
    /**
     * 根据城市分页获取攻略
     * @param pageByCityDTO
     * @return
     */

    @Override
    public PageVO<GetStrategiesByCityVO> getStrategiesByCity(PageByCityDTO pageByCityDTO) {
        PageHelper.startPage(pageByCityDTO.getPage(),pageByCityDTO.getPageSize());
        QueryWrapper<Strategy> qw=new QueryWrapper<>();
        qw.like("city",pageByCityDTO.getCity());
        List<Strategy> list = strategyMapper.selectList(qw);
        GetStrategiesByCityVO gf=new GetStrategiesByCityVO();
        List<GetStrategiesByCityVO> Strategies=new ArrayList<>();
        for(Strategy strategy:list){
            BeanUtils.copyProperties(strategy,gf);
            Strategies.add(gf);
        }
        if (Strategies == null){
            return null;
        }
        PageInfo<GetStrategiesByCityVO> pageInfo = new PageInfo<>(Strategies);
        return new PageVO<>(pageInfo.getTotal(),pageInfo.getList());
    }




    /**
     * Description:分页获取攻略
     * date: 2024/04/01
     * author:王铭杰
     */
    public PageVO<GetAllStrategiesVO> getAllStrategies(PageDTO pageDTO) {
        PageHelper.startPage(pageDTO.getPage(),pageDTO.getPageSize());
        List<GetAllStrategiesVO> list = strategyMapper.getAllStrategies();
        if (list == null){
            return null;
        }
        PageInfo<GetAllStrategiesVO> pageInfo = new PageInfo<>(list);
        return new PageVO<>(pageInfo.getTotal(),pageInfo.getList());
    }


    /**
     * Description:获取攻略详情页
     * date: 2024/04/01
     * author:王铭杰
     */
    public GetStrategyVO getStrategy(Long id) {
        GetStrategyVO getStrategyVO = strategyMapper.getStrategy(id);
        if (getStrategyVO == null) {
            return null;
        }
        //        public static final String[] likeStatus = {"rounte","attractions","strategy","news"};
        String key = LikeContext.likeStatus[2] + id;
        Long userId = BaseContext.getCurrentId();
        boolean identify = Boolean.TRUE.equals(stringRedisTemplate.opsForSet().isMember(key, String.valueOf(userId)));
        Integer likeStatus = identify? 1 : 0;
        getStrategyVO.setLikeStatus(likeStatus);
        return getStrategyVO;
    }


    /**
     * Description:获取点赞量最高的4个攻略
     * date: 2024/04/01
     * author:王铭杰
     */
    public List<GetAllStrategiesVO> getTopStrategy() {
        List<GetAllStrategiesVO> list = strategyMapper.getTopStrategy();
        return list;
    }


    /**
     * Description:通过用户id获取攻略
     * date: 2024/04/01
     * author:王铭杰
     */
    public List<GetAllStrategiesVO> getStrategiesByAuthorId(Long userId) {

        List<GetAllStrategiesVO> list = strategyMapper.getStrategiesByAuthorId(userId);
        return list;
    }

    @Override
    public Result deleteStrategy(Long id) {
        strategyMapper.deleteStrategy(id);
        return Result.success("删除成功！");
    }

    @Override
    public Result updateStrategy(UpdateStrategyDTO updateStrategyDTO) {
        if(strategyMapper.updateStrategy(updateStrategyDTO) > 0){
            return Result.success("修改成功");
        }
        return Result.error("修改失败，未知错误！");
    }
}
